﻿#include "AppFunction.h"
#include "CreateWidget.h"
#include "string.h"


#if defined(DEBUG_IN_PC)
#include <windows.h>
#else
#include "aworks.h"
#include "aw_time.h"
#include "aw_rtc.h"
#include "aw_prj_params.h"
#endif

#include "DIALOG.h"
#include <stdio.h>



#define RTC_ID    RTC_ID0        /* PCF85063 RTC */



void PasswordFunction(pPARA_TCB pBase, pPARA_TCB pPara, uint8_t stage);
void SettingFunction(pPARA_TCB pBase, pPARA_TCB pPara, uint8_t stage);




static pPARA_TCB timeTcb;
static uint8_t week[7][12] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

void TimeCallback(void);




void draw_line(void *pPara) {

	pPARA_TCB pWin = (pPARA_TCB)pPara;
	GUI_COLOR line_color = GUI_MAKE_COLOR(0x3BA2E0);
	GUI_SetColor(line_color);

	switch (pWin->appTCB.funType) {
	case GROUP_CONTROL:
		//GUI_DrawLine()
		GUI_DrawRoundedFrame(20, 110, 700, 700, 20, 2);
		break;
	case GROUP_SET:
		GUI_DrawRoundedFrame(20, 110, 700, 700, 20, 2);
		GUI_DrawLine(60, 200, 660, 200);
		GUI_DrawLine(60, 300, 660, 300);
		GUI_DrawLine(60, 400, 660, 400);
		GUI_DrawLine(60, 500, 660, 500);
		GUI_DrawLine(60, 600, 660, 600);
		break;
	case GROUP_NAV3:
		break;
	case GROUP_NAV4:
		break;
	default:
		break;
	}
}


uint8_t InitAppFunction(void)
{
	timeTcb = NULL;

	SetHomePageTimerCallback(TimeCallback);

	return 1;
}


void SwitchActuator(pPARA_TCB pPara)
{
	pGroupAddressMsg pAddress;
}


void SetTimeBase(uint16_t year, uint16_t month, uint16_t day, uint16_t hour, uint16_t minute, uint16_t second)
{
#if !defined(DEBUG_IN_PC)
	aw_tm_t  time;

	time.tm_sec = second;           //设置秒
	time.tm_min = minute;           //设置分
	time.tm_hour = hour;             //设置小时
	time.tm_mday = day;              //设置日
	time.tm_mon = month - 1;        //设置月，实际月份要减一
	time.tm_year = year - 1900;      //设置年，距1900的年数
	time.tm_isdst = -1;               // 夏令时不可用

	aw_tm_set(&time);
#endif
}


/* Should be called each second */
void TimeCallback(void)
{
	pPARA_TCB pPara = timeTcb;
	uint8_t timeShow[50], dateShow[50];

#if defined(DEBUG_IN_PC)
	SYSTEMTIME time;

	GetLocalTime(&time);

	dateShow[0] = 0;
	sprintf(timeShow, "%02d月%02d日 ", time.wMonth, time.wDay);
	AddStringToBuffer(dateShow, timeShow);
	AddStringToBuffer(dateShow, week[time.wDayOfWeek]);

	sprintf(timeShow, "%02d:%02d", time.wHour, time.wMinute);

#else
	aw_tm_t  time;


	aw_rtc_time_get(RTC_ID, &time);

	sprintf(timeShow, "%02d:%02d", time.tm_hour, time.tm_min);

	sprintf(dateShow, "%02d月%02d日 ", time.tm_mon + 1, time.tm_mday);
	strcat(dateShow, week[time.tm_wday]);
#endif

	while (1)
	{
		if (pPara != NULL)
		{
			if (pPara->guiState == GUI_STATE_VALID)
			{
				if ((pPara->appTCB.funType & NORMAL_MASK) == TIME_FUNCTION)
					SetWidgetText(pPara, timeShow);
				else
					SetWidgetText(pPara, dateShow);
			}

			pPara = pPara->pTimeNext;
		}
		else
			break;
}
}


void AddTimeFunction(pPARA_TCB pPara)
{
	GUI_LOCK();

	if (timeTcb == NULL)
		timeTcb = pPara;
	else
	{
		pPara->pTimeNext = timeTcb;
		timeTcb = pPara;
	}

	GUI_UNLOCK();
}


uint8_t CallFunction(pPARA_TCB pBase, pPARA_TCB pPara, uint8_t stage, uint32_t data)
{
	uint16_t type;

	if (stage == 2 && pBase->guiTCB.specialInfo.encryption.cipherTimer == data)             // 定时器回调
	{
		PasswordFunction(pBase, pPara, stage);
		return 0;
	}

	type = (pPara->appTCB.funType & NORMAL_MASK);
	if (type > ENCRYPTION_FUNCTION)
		type = (pBase->appTCB.funType & NORMAL_MASK);

	switch (type)
	{
	case SWITCH_ACTUATOR:
		SwitchActuator(pPara);
		break;
	case DIMMER_ACTUATOR:
		break;
	case SHUTTER_ACTUATOR:
		break;
	case IO_BUTTON_SENSOR:
		break;
	case SET_TIME_FUNCTION:
		if (pPara->appTCB.keyMsg == WM_NOTIFICATION_RELEASED)
			SetTimeBase(2019, 6, 6, 9, 0, 0);

		break;
	case ENCRYPTION_FUNCTION:
		PasswordFunction(pBase, pPara, stage);
		break;
	case LANGUAGE_FUNCTION:
		if (pPara->appTCB.keyMsg == WM_NOTIFICATION_RELEASED)
		{
			SetLanguage(LANGUAGE_ENGLISH);
		}
	case GROUP_SET:
		SettingFunction(pBase, pPara, stage);
		break;
	default:
		break;
	}

	return 0;
}